package jp.aist.gtrc.plus.admission.command;

import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.io.Serializable;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import jp.aist.gtrc.plus.admission.*;


/*
 * Issue qdel to delete jobs from PBS and instruct admission control to 
 *  reschedule.
 */
public class JobDeleter extends UnicastRemoteObject implements QcommandClient {

        public static final int COMM_DELAY = 15;  // seconds allowed for communication delay
        private static String hostname = "localhost";

        private String jobId;

        public JobDeleter(String jobId) throws RemoteException {
           this.jobId = jobId;
        }

        protected static AdmissionServer getAdmissionServer() throws AdmissionException {
           if (System.getSecurityManager() == null) 
              System.setSecurityManager(new AdmissionSecurityManager()); 

           int rmiPort = Registry.REGISTRY_PORT;

           try {
              Registry registry = LocateRegistry.getRegistry(hostname,rmiPort);
//System.out.println(registry);
              AdmissionServer admissionServer = (AdmissionServer) registry.lookup(AdmissionServer.RMI_OBJ_NAME);
              return admissionServer;
           } catch (Exception e) {
              throw new AdmissionException("Cannot connect to rmi://" + hostname + ":" + rmiPort);
           }
        } 

   public static void main(String[] args) throws Exception {

      JobDeleter jobDeleter = null;
      AdmissionServer admissionServer = null;
      try {
         String jobId = args[0];
         jobDeleter = new JobDeleter(jobId); 

      } catch (Exception e) {
         e.printStackTrace();
         usage();
         System.exit(1);
      }


      try {
         if (args.length == 2) {
            hostname = args[1];
         }
         admissionServer = getAdmissionServer();
         admissionServer.reschedule(jobDeleter);
         System.exit(0);
         
      } catch (Exception e) {
         e.printStackTrace();
         //System.err.println(e);
         System.exit(1);
      }

   }

        public String qcommand() { // callback method
           try {
              String cmd = "qdel " + jobId;
//System.out.println(cmd);
               
              Process p = Runtime.getRuntime().exec(cmd);
              BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

              BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

              // read the output from the command
              //System.out.println("Here is the standard output of the command:\n");
              String s = null;
              while ((s = stdInput.readLine()) != null) {
                 System.out.println(s);
              }
            
              // read any errors from the attempted command
              //System.out.println("Here is the standard error of the command (if any):\n");
              boolean hasError = false;
              while ((s = stdError.readLine()) != null) {
                 System.err.println(s);
                 hasError = true;
              }
              if (hasError) throw new RuntimeException(s);

           }
           catch (Exception e) {
              //e.printStackTrace();
              throw new RuntimeException(e);
           }
   
           return "";
        }

        public String qcommand(String argument) {
           throw new RuntimeException("Arbitrary Qcommand not support");
        }

        public static void usage() {
           System.err.println("Usage: classname  <jobid> [hostname]");
        }

}
